package com.uduew.sjd.ui.agreement

import android.content.Intent
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.ui.base.BaseActivity
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.ResUtil
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.kongzue.dialogx.dialogs.PopTip
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State
import com.uduew.sjd.config.AppConfig
import com.uduew.sjd.config.Const

class PrivacyActivity : BaseActivity() {
    private lateinit var mStates: PrivacyStates

    override fun initViewModel() {
        mStates = getActivityScopeViewModel(PrivacyStates::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.activity_privacy, BR.vm, mStates)
            .addBindingParam(
                BR.click,
                ClickProxy()
            ).addBindingParam(BR.titleBarListener, TitleBarListener())
    }

    inner class TitleBarListener : OnTitleBarListener {
        override fun onLeftClick(titleBar: TitleBar?) {
            super.onLeftClick(titleBar)
            finish()
        }

        override fun onRightClick(titleBar: TitleBar?) {
            super.onRightClick(titleBar)
        }
    }


    open inner class ClickProxy {
        fun privacyPolicy() {
            openUrlInWebView(mStates.url.get(), getString(R.string.privacy_policy))
        }

        fun agreementChecked() {
            mStates.agreementChecked.get()?.let { mStates.agreementChecked.set(!it) }
        }


        fun userAgreement() {
            openUrlInWebView(mStates.url.get(), getString(R.string.user_agreement_empty))
        }

        fun agreement() {
            if (mStates.agreementChecked.get() == false) {
                PopTip.show(getString(R.string.privacy_policy_tip))
                return
            }
            AppConfig.mConfigs.isPrivacy.set(true)
            this@PrivacyActivity.startActivity(
                Intent(
                    this@PrivacyActivity,
                    PermissionActivity::class.java
                )
            )
            finish()
        }

        fun disagree() {
            finish()
        }


    }

    class PrivacyStates : StateHolder() {
        val agreementChecked: State<Boolean> = State(true)
        val url: State<String> = State(Const.PRIVACY_URL)
        val privacyPolicy: State<String> = State(ResUtil.getString(R.string.privacy_policy))
        val userAgreement: State<String> = State(ResUtil.getString(R.string.user_agreement))
    }
}